package com.client.controller;

import com.client.utils.ResponseMsg;
import org.springframework.web.bind.annotation.*;

import com.client.service.EntryCollectsService;

import jakarta.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import java.util.Map;

/**
 * <p>
 * 词条收藏记录表 前端控制器
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 08:28:54
 */
@RestController
@RequestMapping("/entryCollects")
public class EntryCollectsController {
    @Autowired
    private EntryCollectsService entryCollectsService;

    //根据用户ID和词条ID查询是否已经收藏
    @PostMapping("/searchIfCollected")
    public String searchIfCollected(@RequestBody Map<String, Object> param,HttpServletRequest request) {
        Integer userId = (Integer) param.get("userId");
        Integer entryId = (Integer) param.get("entryId");
        if(entryCollectsService.searchIfCollected(userId,entryId)){
            return JSON.toJSONString(true);
        }
        return JSON.toJSONString(false); 
    }

    //根据用户ID和词条ID,作者ID增加收藏
    @PostMapping("/addCollects")
    public String addCollects(@RequestBody Map<String, Object> param,HttpServletRequest request){
        Integer userId = (Integer) param.get("userId");
        Integer entryId = (Integer) param.get("entryId");
        Integer authorId = (Integer) param.get("authorId");
        entryCollectsService.addCollects(userId,entryId,authorId);
        return JSON.toJSONString(true);
    }
    //根据用户ID和词条ID取消收藏
    @PostMapping("/cancelCollects")
    public String cancelCollects(@RequestBody Map<String, Object> param,HttpServletRequest request){
        Integer userId = (Integer) param.get("userId");
        Integer entryId = (Integer) param.get("entryId");
        entryCollectsService.cancelCollects(userId,entryId);
        return JSON.toJSONString(true);
    }
    @GetMapping("/getEntryById/{userId}")
    public ResponseMsg getEntryById (@PathVariable Integer userId) {
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,
                "查询完成",entryCollectsService.getEntryById(userId));
    }
}
